package com.itsenpupulai.bdtest.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.route.BikingRouteLine;
import com.baidu.mapapi.search.route.BikingRoutePlanOption;
import com.baidu.mapapi.search.route.BikingRouteResult;
import com.baidu.mapapi.search.route.DrivingRoutePlanOption;
import com.baidu.mapapi.search.route.DrivingRouteResult;
import com.baidu.mapapi.search.route.IndoorRouteResult;
import com.baidu.mapapi.search.route.MassTransitRouteResult;
import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener;
import com.baidu.mapapi.search.route.PlanNode;
import com.baidu.mapapi.search.route.RoutePlanSearch;
import com.baidu.mapapi.search.route.TransitRouteResult;
import com.baidu.mapapi.search.route.WalkingRouteResult;
import com.itsenpupulai.bdtest.R;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class RoutPlanActivity extends AppCompatActivity  {

    private TextView tv_all;
    private StringBuilder sb;
    private RoutePlanSearch search;
    private Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rout_plan);
        tv_all = (TextView) findViewById(R.id.tv_all);
        btn = (Button) findViewById(R.id.btn_jisuan);

        sb = new StringBuilder();

        //116.307689,40.056974  百度大厦
        //116.403963,39.915119
        sb.append("起点：" + "40.056974, 116.307689 \n" + "终点：" + "39.915119, 116.403963 \n");
        tv_all.setText(sb);

               //百度的搜索路线的类


       btn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               long time = new Date().getTime();
               searchRoute(40.056974, 116.307689 , 39.915119, 116.403963 );

               SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
               sb.append("开始时间" + dateFormat.format(time) + "\n");
           }
       });

    }


    private void searchRoute(double d1, double d2, double d3, double d4) {
        search = RoutePlanSearch.newInstance();
        search.setOnGetRoutePlanResultListener(new OnGetRoutePlanResultListener() {
            @Override
            public void onGetWalkingRouteResult(WalkingRouteResult walkingRouteResult) {
            }

            @Override
            public void onGetTransitRouteResult(TransitRouteResult transitRouteResult) {
            }

            @Override
            public void onGetMassTransitRouteResult(MassTransitRouteResult massTransitRouteResult) {
            }

            @Override
            public void onGetDrivingRouteResult(DrivingRouteResult drivingRouteResult) {
            }

            @Override
            public void onGetIndoorRouteResult(IndoorRouteResult indoorRouteResult) {

            }

            @Override
            public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
                if (bikingRouteResult.getRouteLines() == null) {
                    Toast.makeText(RoutPlanActivity.this, "骑行算路失败", Toast.LENGTH_SHORT).show();

                    return;
                }
                int duration  = 0;
                List<BikingRouteLine> lines = bikingRouteResult.getRouteLines();
                for (int i = 0; i < lines.size(); i++) {
                    duration += lines.get(i).getDistance();
                }
                long time = new Date().getTime();
                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                sb.append("结束时间" + dateFormat.format(time) + "\n");
                sb.append("骑行：" + duration + "\n");
                tv_all.setText(sb);
            }
        });


        DrivingRoutePlanOption drivingRoutePlanOption = new DrivingRoutePlanOption();
        //起始坐标和终点坐标
        LatLng latLng = new LatLng(d1, d2); //LatLng(double latitude, double longitude)
        LatLng latLngend = new LatLng(d3, d4);
        PlanNode startPlanNode = PlanNode.withLocation(latLng) ;
        PlanNode endPlanNode = PlanNode.withLocation(latLngend);
        search.bikingSearch(new BikingRoutePlanOption().from(startPlanNode).to(endPlanNode));

    }


}
